﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frw.Configuration;
using System.Configuration;

namespace Frw.Protocols.Communications.Tcp.Configuration
{
    /// <summary>
    /// Communicator TCP configuration section...
    /// </summary>
    /// <remarks>
    /// 
    ///  La sezione di configurazione nel file .config deve essere impostata come l'esempio seguente:
    ///  
    ///  <configSections>
    ///    <sectionGroup  name="MySectionGroup">
    ///      <section name="MySection" type="Frw.Protocols.Communications.Tcp.Configuration.CommunicatorSection, Frw.Protocols.Communications" allowLocation="true" allowDefinition="Everywhere" />
    ///    </sectionGroup>
    ///  </configSections>
    ///  
    ///  <MySectionGroup>
    ///    <CommunicatorSection>
    ///      <CommunicatorElements>
    ///        <add key="client" ipAddress="127.0.0.1" port="7734" />
    ///        <add key="server" ipAddress="127.0.0.1" port="7735" />
    ///      </CommunicatorElements>
    ///    </CommunicatorSection>
    ///  </MySectionGroup>
    ///  
    /// </remarks>
    [Serializable]
    public class CommunicatorSection : ConfigurationSection<CommunicatorSection>
    {
        [ConfigurationProperty("CommunicatorElements", IsDefaultCollection = false),
         ConfigurationCollection(typeof(ConfigurationCollection<CommunicatorElement>), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
        public ConfigurationCollection<CommunicatorElement> CommunicatorElements
        {
            get { return (ConfigurationCollection<CommunicatorElement>)this["CommunicatorElements"]; }
        }

        /// <summary>
        /// Section Handler for the TCP Communicator Config Section...
        /// </summary>
        public CommunicatorSection()
        {

        }

        public CommunicatorElement GetElement(string key)
        {
            CommunicatorElement retValue = null;

            retValue = this.CommunicatorElements[key];

            return retValue;
        }

        public CommunicatorElement GetElement2(string key)
        {
            CommunicatorElement retValue = null;

            foreach (CommunicatorElement ce in this.CommunicatorElements)
            {
                if (ce.Key == key)
                {
                    retValue = ce;
                    break;
                }
            }

            return retValue;
        }
    }
}
